package com.meizhao.admin.service;


import com.meizhao.admin.configuration.ConfigProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;


@Slf4j
@Service
public class FileService {

    private Environment environment;

    private ConfigProperties configProperties;

    @Autowired
    public FileService(
            ConfigProperties configProperties,
            Environment environment){
        this.environment = environment;
        this.configProperties = configProperties;
    }

    /*public List<BaseFile> saveFiles(MultipartFile[] files, Long referenceId){

        List<BaseFile> list = new ArrayList<>(files.length*2);
        for(MultipartFile file : files) {
            list.add(saveFile(file, referenceId, false));
        }
        return list;
    }

    public BaseFile saveFile(MultipartFile file, Long referenceId){

        return saveFile(file, referenceId, false);
    }

    public BaseFile saveAvatar(MultipartFile file, Long referenceId){

        return saveFile(file, referenceId, true);
    }

    private BaseFile saveFile(MultipartFile file, Long referenceId, Boolean isAvatar){

        BaseFile baseFile = new BaseFile();
        String name = file.getOriginalFilename();
        baseFile.setFileName(name);
        baseFile.setFileSize(file.getSize());
        try {
            baseFile.setMimeType(Utils.getFileType(configProperties.getFileTypeLimits(), IOUtils.toByteArray(file.getInputStream())));
            baseFile.setFileSuffix(baseFile.getMimeType());
            if(referenceId == null) {
                baseFile.setReferenceId(ThreadLocalRandom.current().nextLong());
            }else{
                baseFile.setReferenceId(referenceId);
            }
            String filePath = isAvatar ? saveAvatar(file.getInputStream(), baseFile.getFileSuffix()):
                saveFile(file.getInputStream(), baseFile.getFileSuffix());
            baseFile.setFileUrl(filePath);
            baseFileMapper.insert(baseFile);
            return baseFile;
        }catch (IOException e){
            log.error(e.getMessage());
            throw new CommonException(ResultCode.EXCEPTION, "上传失败,请重新选择文件");
        }
    }

    public void delPic(Long id) {

        baseFileMapper.deleteById(id);
    }



    public String saveFile(InputStream inputStream, String fileSuffix) {

        String child = Clock.systemUTC().millis()+ ThreadLocalRandom.current().nextLong()+ fileSuffix;
        String mediaFolderPath;
        if(environment.getProperty("os.name").contains("Windows")){
            mediaFolderPath = configProperties.getFileWinPath();
        }else{
            mediaFolderPath = configProperties.getFileLinuxPath();
        }
        try {
            FileUtils.copyInputStreamToFile(inputStream, new File(mediaFolderPath, child));
            return configProperties.getFilePattern() + child;
        }catch (Exception e){
            e.printStackTrace();
            throw new CommonException(ResultCode.EXCEPTION, "上传失败,请重新选择文件");
        }
    }


    public Long saveFileAndGetRerferenceId(InputStream inputStream, String fileSuffix) {

        BaseFile baseFile = new BaseFile();
        Long child1 = Clock.systemUTC().millis()+ ThreadLocalRandom.current().nextLong();
        baseFile.setReferenceId(child1);
        String child = child1 + fileSuffix;
        baseFile.setFileName(child);

        String mediaFolderPath;
        if(environment.getProperty("os.name").contains("Windows")){
            mediaFolderPath = configProperties.getFileWinPath();
        }else{
            mediaFolderPath = configProperties.getFileLinuxPath();
        }
        try {
            FileUtils.copyInputStreamToFile(inputStream, new File(mediaFolderPath, child));
            String filePath = configProperties.getFilePattern() + child;
            baseFile.setFileUrl(filePath);
            baseFileMapper.insert(baseFile);
            System.out.println("插入文件");
            return child1;
        }catch (Exception e){
            e.printStackTrace();
            throw new CommonException(ResultCode.EXCEPTION, "上传失败,请重新选择文件");
        }
    }

    private String saveAvatar(InputStream inputStream, String fileSuffix) {

        String child = Clock.systemUTC().millis()+ ThreadLocalRandom.current().nextLong() + fileSuffix;
        String mediaFolderPath;
        if(environment.getProperty("os.name").contains("Windows")){
            mediaFolderPath = configProperties.getFileWinAvatarPath();
        }else{
            mediaFolderPath = configProperties.getFileLinuxAvatarPath();
        }
        try {
            FileUtils.copyInputStreamToFile(inputStream, new File(mediaFolderPath, child));
            return configProperties.getAvatarPattern() + child;
        }catch (Exception e){
            e.printStackTrace();
            throw new CommonException(ResultCode.EXCEPTION, "上传失败,请重新选择文件");
        }
    }

    public List<BaseFile> getFileList(long referenceId) {

        QueryWrapper<BaseFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("reference_id", referenceId);
        return baseFileMapper.selectList(queryWrapper);
    }*/
}
